package com.example.demo02.service.impl;


import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.example.demo02.constants.CommonConstant;
import com.example.demo02.dao.ShopCouponMapper;
import com.example.demo02.dao.TcDiscountMapper;
import com.example.demo02.dto.CouponSumSimpleInfo;
import com.example.demo02.dto.ShopCouponQryInfo;
import com.example.demo02.dto.ShopCouponRespInfo;
import com.example.demo02.entity.TcDiscount;
import com.example.demo02.form.ShopCouponForm;
import com.example.demo02.mapstruct.EntityToVoStructMapper;
import com.example.demo02.service.ShopCouponService;
import com.example.demo02.service.TcDiscountService;
import com.example.demo02.utils.PageUtils;
import com.example.demo02.vo.input.ShopCouponSendVo;
import com.example.demo02.vo.result.ShopCouponResultVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ShopCouponServiceImpl implements ShopCouponService {
    @Resource
    ShopCouponMapper shopCouponMapper;
    @Resource
    EntityToVoStructMapper entityToVoStructMapper;
    @Resource
    TcDiscountService discountService;


    @Override
    public PageInfo<ShopCouponRespInfo> getShopCouponListByPage(ShopCouponForm shopCouponForm) {

        PageHelper.startPage(shopCouponForm.getPageNum(),shopCouponForm.getLength());
        List<ShopCouponQryInfo> list = shopCouponMapper.getShopCouponListByPage(shopCouponForm);
//        PageInfo<ShopCouponQryInfo> of = PageInfo.of(list);
        //修改了一些代码
        PageInfo<ShopCouponQryInfo> of = PageInfo.of(list);
        Map<String, CouponSumSimpleInfo> map=getCouponSumSimpleMap(list);
        return PageUtils.mapping(of,node->ShopCouponRespInfo.from(node,map));
    }

    @Override
    public PageInfo<ShopCouponResultVo> getPurchaseListByPage(ShopCouponSendVo shopCouponSendVo) {
        List<TcDiscount> list=discountService.getListOnPurchase(shopCouponSendVo);
        List<ShopCouponResultVo> list1= entityToVoStructMapper.tcDiscountToShopCouponResultVoList(list);
        return PageInfo.of(list1);
    }

    private Map<String, CouponSumSimpleInfo> getCouponSumSimpleMap(List<ShopCouponQryInfo> list) {
        List<String> orderidsList = list.stream().filter(ObjectUtil::isNotNull).map(ShopCouponQryInfo::getOrderids).filter(StrUtil::isNotBlank).collect(Collectors.toList());
        Map<String,CouponSumSimpleInfo> map=new HashMap<>();
        list.forEach(t->{
            List<String> orderidList = StrUtil.split(t.getOrderids(), CommonConstant.SEPARATOR_COMMA);
            //加了点东西
            System.out.println("aaaa");
            CouponSumSimpleInfo couponSumSimpleInfo = new CouponSumSimpleInfo();
            //已使用数量 使用过期数量
            CouponSumSimpleInfo couponSumSimpleInfo1=shopCouponMapper.getUsedSumByOrderids(orderidList);
            //发放过期数量
            CouponSumSimpleInfo couponSumSimpleInfo2=shopCouponMapper.getGrantExpSumByOrderids(orderidList);
            couponSumSimpleInfo.setGrantExpSum(Opt.ofNullable(couponSumSimpleInfo2).map(CouponSumSimpleInfo::getGrantExpSum).get());
            couponSumSimpleInfo.setUsedSum(Opt.ofNullable(couponSumSimpleInfo1).map(CouponSumSimpleInfo::getUsedSum).get());
            couponSumSimpleInfo.setUsedExpSum(Opt.ofNullable(couponSumSimpleInfo1).map(CouponSumSimpleInfo::getUsedExpSum).get());
            String stockSum = NumberUtil.toStr(NumberUtil.sub(NumberUtil.isNumber(t.getPurchaseSum()) ? t.getPurchaseSum() : "0"
                    , NumberUtil.isNumber(couponSumSimpleInfo.getGrantExpSum()) ? couponSumSimpleInfo.getGrantExpSum() : "0",
                    NumberUtil.isNumber(t.getGrantSum()) ? t.getGrantSum() : "0"));
            couponSumSimpleInfo.setStockSum(stockSum);
            map.put(t.getOrderids(),couponSumSimpleInfo);
        });
        return map;
    }
}
